package com.example.kotlincoroutines.kotlin

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext

/**
 * 模拟安卓网络请求
 */
fun main() {

    val i = runBlocking(Dispatchers.IO) {
        request {
            runBlocking {
                withContext(Dispatchers.Main) {
                    println("切换到主线程更新UI")
                }
            }
        }
    }

    println("world $i")
}

suspend fun request(finish: suspend () -> Unit) {
    delay(3000)
    println("网络请求成功:+${Thread.currentThread().name}")
    finish()
}